package handler;

import entity.GradeDO;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import req.GradeRequest;
import res.GradeDTO;
import util.DBUtil;
//import util.ExcelUtil;
import view.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Vector;

public class TeacherViewHandler extends KeyAdapter implements ActionListener {
    private MainCourseView mainCourseView;
    private Main main;

    public TeacherViewHandler(MainCourseView mainCourseView) {
        this.mainCourseView = mainCourseView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("学生信息录入".equals(text)) {
            //System.out.println("123");
            //new LoginView();
            new MainView();
            //main.dispose();
            System.out.println("学生信息录入");
        } else if ("课程信息录入".equals(text)) {
            new CourseView();
            //main.dispose();
            System.out.println("课程信息录入");
        } else if ("学生成绩录入".equals(text)) {
            new GradeView();
            System.out.println("学生成绩录入");
        } else if ("成 绩  导 出".equals(text)) {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from grade ");
            PreparedStatement ps = null;
            Connection conn = null;
            ResultSet rs = null;
            try {
                conn = DBUtil.getConn();
                ps = conn.prepareStatement(sql.toString());
                rs = ps.executeQuery();
                int i = 0;
                FileOutputStream fileOut = new FileOutputStream("data3.xlsx");
                Workbook workbook = new XSSFWorkbook();
                Sheet sheet = workbook.createSheet("Sheet1");
                Row headerRow = sheet.createRow(i);
                Row row;
                headerRow.createCell(0).setCellValue("学号");
                headerRow.createCell(1).setCellValue("课程号");
                headerRow.createCell(2).setCellValue("分数");
                headerRow.createCell(3).setCellValue("等级");
                headerRow.createCell(4).setCellValue("学期");

                while (rs.next()) {
                    //int ID = rs.getInt("ID");
                    int sno = rs.getInt("sno");
                    int cno = rs.getInt("cno");
                    String grade = rs.getString("grades");
                    String level = rs.getString("level");
                    String semester = rs.getString("semester");

                    //System.out.println(semester);

                    row = sheet.createRow(i + 1);
                    Cell cell = row.createCell(0);
                    cell.setCellValue(sno);

                    Cell cell2 = row.createCell(1);
                    cell2.setCellValue(cno);

                    Cell cell3 = row.createCell(2);
                    cell3.setCellValue(grade);

                    Cell cell4 = row.createCell(3);
                    cell4.setCellValue(level);

                    Cell cell5 = row.createCell(4);
                    cell5.setCellValue(semester);

                    i++;

                }
                try {
                    workbook.write(fileOut);
                    JOptionPane.showMessageDialog(null, "Excel 文件导出成功！");
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null, "Excel 文件导出失败：" + ex.getMessage());
                }
                workbook.close();
                fileOut.close();
            } catch (Exception ex1) {
                ex1.printStackTrace();
            } finally {
                DBUtil.closeRs(rs);
                DBUtil.closePs(ps);
                DBUtil.closeConn(conn);
            }
        }
    }
}
